package com.ruoyi.ai.controller;

import com.ruoyi.chatgpt.service.IGptOrderService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.PayTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @description:OrderController
 * @author: caoes
 * create:2023/6/30 9:37
 **/
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IGptOrderService gptOrderService;

    @ApiOperation(value = "confirm", notes = "结算确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tariffPackageId", value = "资费套餐包", required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "discountCode", value = "折扣码", required = false, dataType = "String")
    })
    @PostMapping(value = "confirm")
    public AjaxResult confirm(Long tariffPackageId, String discountCode) {
        // 参数校验
        if (Objects.isNull(tariffPackageId)) {
            return AjaxResult.error("资费套餐不能为空");
        }
        Map<String, Object> result = gptOrderService.doConfirm(tariffPackageId, discountCode);
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "submit", notes = "提交订单结算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tariffPackageId", value = "资费套餐包ID", required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "discountCode", value = "折扣码", required = false, dataType = "String"),
            @ApiImplicitParam(name = "payType", value = "支付方式", required = true, dataType = "PayTypeEnum")
    })
    @PostMapping(value = "submit")
    public AjaxResult submit(
            Long tariffPackageId,
            String discountCode,
            PayTypeEnum payType) {
        // 参数校验
        if (Objects.isNull(tariffPackageId)) {
            return AjaxResult.error("资费套餐不能为空");
        }
        if (Objects.isNull(payType)) {
            return AjaxResult.error("请选择支付方式");
        }
        Map<String, Object> result = gptOrderService.doSubmit(tariffPackageId, discountCode, payType);
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "queryOrder", notes = "查询订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "String")
    })
    @GetMapping(value = "/{orderCode}")
    public AjaxResult queryOrder(@PathVariable String orderCode) {
        // 参数校验
        if (StringUtils.isBlank(orderCode)) {
            return AjaxResult.error("订单号不能为空");
        }

        Map<String,Object> result = gptOrderService.queryOrder(orderCode);
        return AjaxResult.success(result);
    }
}
